package com.example.maoyanmovie.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;
import com.example.maoyanmovie.R;
import com.example.maoyanmovie.databinding.ActivityGuideBinding;
import com.example.maoyanmovie.databinding.ItemGuideBinding;
import com.example.maoyanmovie.utils.Constants;
import com.example.maoyanmovie.utils.SPUtils;

public class GuideActivity extends AppCompatActivity {

    private ActivityGuideBinding binding;
    private String[] titles = {"发现精彩电影", "随时在线选座", "畅享观影体验"};
    private String[] descs = {"海量热映大片，总有一款适合你", "便捷购票流程，轻松完成订座", "优质服务体验，享受电影时光"};
    private int[] images = {R.drawable.baseline_ondemand_video_24, R.drawable.ic_launcher, R.drawable.baseline_sentiment_very_satisfied_24};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityGuideBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initViewPager();
        initIndicator();
        initListeners();
    }

    private void initViewPager() {
        binding.viewPager.setAdapter(new RecyclerView.Adapter<GuideViewHolder>() {
            @NonNull
            @Override
            public GuideViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                ItemGuideBinding itemBinding = ItemGuideBinding.inflate(getLayoutInflater(), parent, false);
                return new GuideViewHolder(itemBinding);
            }

            @Override
            public void onBindViewHolder(@NonNull GuideViewHolder holder, int position) {
                holder.binding.ivGuide.setImageResource(images[position]);
                holder.binding.tvTitle.setText(titles[position]);
                holder.binding.tvDesc.setText(descs[position]);
            }

            @Override
            public int getItemCount() {
                return titles.length;
            }
        });
    }

    private void initIndicator() {
        for (int i = 0; i < titles.length; i++) {
            ImageView dot = new ImageView(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20);
            params.setMargins(8, 0, 8, 0);
            dot.setLayoutParams(params);
            dot.setImageResource(i == 0 ? R.drawable.indicator_selected : R.drawable.indicator_unselected);
            binding.llIndicator.addView(dot);
        }
    }

    private void initListeners() {
        binding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                updateIndicator(position);
                binding.btnStart.setVisibility(position == titles.length - 1 ? View.VISIBLE : View.GONE);
            }
        });

        binding.btnStart.setOnClickListener(v -> {
            SPUtils.putBoolean(Constants.SP_KEY_FIRST_LAUNCH, false);
            startActivity(new Intent(GuideActivity.this, LoginActivity.class));
            finish();
        });
    }

    private void updateIndicator(int position) {
        for (int i = 0; i < binding.llIndicator.getChildCount(); i++) {
            ImageView dot = (ImageView) binding.llIndicator.getChildAt(i);
            dot.setImageResource(i == position ? R.drawable.indicator_selected : R.drawable.indicator_unselected);
        }
    }

    static class GuideViewHolder extends RecyclerView.ViewHolder {
        ItemGuideBinding binding;

        GuideViewHolder(ItemGuideBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
    }
}
